﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MishraReader.Entities
{
    internal static class DateTimeExtensions
    {
        public static DateTime FromUSecUnixTime(this string dateString)
        {
            dateString = dateString.Substring(0, dateString.Length - 6);
            return new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddSeconds(Int64.Parse(dateString)).ToLocalTime();
        }

        public static DateTime FromUnixTime(this string dateString)
        {
            return new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddSeconds(Int64.Parse(dateString)).ToLocalTime();
        }

        public static long ToUnixTime(this DateTime dateTime)
        {
            if (dateTime == DateTime.MinValue)
            {
                return 0;
            }


            var epoc = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
            var delta = dateTime.ToUniversalTime() - epoc;

            return (long)(delta.TotalMilliseconds * 1000);
        }
    }
}